'use strict'

let gulp = require( 'gulp' ),
    {{projectStyle}} = require( 'gulp-{{projectStyle}}' ),
    server = require( 'gulp-server-livereload' ),
    sourcemaps = require( 'gulp-sourcemaps' ),
    babel = require( 'gulp-babel' ),
    rename = require( 'gulp-rename' ),
    colors = require( 'colors' );

colors.setTheme({
    'suc': 'green'
});

gulp.task('default', ['es6', 'css', 'server'] , function() {
   gulp.watch(['src/css/style.{{projectStyleExtend}}', 'src/css/common.{{projectStyleExtend}}'], ['css'])
       .on('change', function(){
           console.log( '{{projectStyle}} rebuild success'.suc );
       });

    gulp.watch(['src/js/script.{{projectScriptExtend}}'], ['es6'])
        .on('change', function(){
            console.log( '{{projectScript}} rebuild success'.suc );
        });
});

gulp.task('css', function() {
    gulp.src('src/css/style.{{projectStyleExtend}}')
        .pipe(sourcemaps.init())
        {{projectStyleLogErr}}
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest('src/dist/css'));
});

gulp.task('server', function() {
    gulp.src('./')
        .pipe(server({
            host: '0.0.0.0',
            directoryListing: {enable: true},
            defaultFile: 'index.html',
            livereload: {
                enable: true,
                filter: function(filePath, cb) {
                    cb( !(/node_modules/.test(filePath)) );
                }
            },
            open: true
        }))
});

gulp.task('es6', function(){
    gulp.src(['src/js/*.{{projectScriptExtend}}'])
        .pipe(babel({ blacklist: ["useStrict"] }))
        .pipe(rename(function( path ){
            path.basename += '.es5';
            path.extname = '.js';
            return path;
        }))
        .pipe(gulp.dest('src/dist/js'));
});
